<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GestionDb
 *
 * @author carlos
 */
abstract class GestionDb {
    
    private $conexion;
    
    public static function consultaLibre($query)
    {
        $conexionAux=new Conexion("localhost","tramitedb","postgres","1234","5432");
        $consulta = $conexionAux->ejecutarConsulta($query);
        return $consulta;        
    }    
    
    function __construct() {
        $this->conexion = new Conexion("localhost","tramitedb","postgres","1234","5432");
        $this->conexion->ejecutarConsulta("select * from usuario");
        //new Conexion($host, $dbName, $user, $password)
    }
    
    protected abstract function getTabla();    
    protected abstract function getDatos();
    
    public function grabar()
    {
       $consulta="INSERT INTO ".$this->getTabla()."(";
       $vector=$this->getDatos();
       
    
       for ($index = 0; $index < count($vector); $index++) 
       {
           if($index==count($vector)-1)
               $consulta=$consulta.$vector[$index][0];    
           else     
                $consulta=$consulta.$vector[$index][0].",";
       }
       $consulta=$consulta.") VALUES (";
       
       
       for ($index = 0; $index < count($vector); $index++) 
       {
           if($index==count($vector)-1)
               $consulta=$consulta."'".$vector[$index][1]."'";    
           else     
                $consulta=$consulta."'".$vector[$index][1]."',";
       }
       
       $consulta=$consulta.");";
       echo $consulta;
       $Result1= $this->conexion->ejecutarConsulta($consulta);
             
       //return $Result1;       
    } 
    
    public function editar()
    {
        $consulta = "UPDATE " . $this->getTabla() . " SET ";
        $vector = $this->getDatos();


        for ($index = 0; $index < count($vector); $index++) {
            if ($index == count($vector) - 1)
                $consulta = $consulta . $vector[$index][0] . "='" . $vector[$index][1] . "'";
            else
                $consulta = $consulta . $vector[$index][0] . "='" . $vector[$index][1] . "'" . ",";
        }
        $consulta = $consulta . " WHERE " . $vector[0][0] . "='" . $vector[0][1] . "';";
        // echo $consulta;            
        $Result1 = $this->conexion->ejecutarConsulta($consulta, $this->conexion) or die(mysql_error());

        return $Result1;
    }
    
    public function eliminar()
    {
        $vector = $this->getDatos();
        $consulta = "DELETE FROM " . $this->getTabla();
        $consulta = $consulta . " WHERE " . $vector[0][0] . "='" . $vector[0][1] . "';";
        //echo $consulta;
        $Result1 = $this->conexion->ejecutarConsulta($consulta, $this->conexion) or die(mysql_error());

        return $Result1;
    }
    
    public function consulta()
    {
        $vector = $this->getDatos();
        if (empty($vector[0][1])) {
            $query = "SELECT * FROM " . $this->getTabla();
        } else {
            $query = "SELECT * FROM " . $this->getTabla(). " WHERE " . $vector[0][0] . "='" . $vector[0][1] . "';";
        }

        $consulta = $this->conexion->ejecutarConsulta($query);
        //echo "Query" . $query;
        return $consulta;
    }
    
    
  
        
}
